AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

function ENT:Initialize()

	self.Entity:SetModel( "models/props_combine/combine_mine01.mdl" ) 
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake() 
	end 
	
	self.Inputs = Wire_CreateInputs( self.Entity, { "X", "Y", "Z", "Launch"} )

	self.reloadtime = CurTime()
	self.launch = false
	self.XCo = 0
	self.YCo = 0
	self.ZCo = 0
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "missilelauncher" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	return ent
	
end

function ENT:TriggerInput(iname, value)
	if (iname == "X") then
		self.XCo = value

	elseif (iname == "Y") then
		self.YCo = value

	elseif (iname == "Z") then
		self.ZCo = value

	elseif (iname == "Launch") then
		if value == 1 then
			self.launch = true
		else
			self.launch = false
		end
	end
end

function ENT:Launch()

local NewRock = ents.Create( "guided_missile" )
		NewRock:SetPos( self.Entity:GetPos() + self.Entity:GetUp() * 10 )
		NewRock:SetAngles( self.Entity:GetUp():Angle() )
		NewRock.ParL = self.Entity
		NewRock:Spawn()
		NewRock:Activate()
		
		self.reloadtime = CurTime() + self.cooldown
		
end

function ENT:Think()
	if self.launch and self.reloadtime < CurTime() then
		self:Launch()
	end
end

function ENT:Setup(power)
self.power = math.Clamp(power, 1, 50)
self.cooldown = math.Clamp(power / 15, .7, 3.3) 
end